package com.launchrock.projectadvisor.skeleton_BGA;


import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.annotation.SuppressLint;
import android.util.Log;

import java.lang.Math;


public class Player extends GameObject implements IOnSceneTouchListener {

	// ===========================================================
	// Constants
	// ===========================================================
	float mVel = 100;
	
	// ===========================================================
	// Fields
	// ===========================================================
	public float xTouchDown=0,yTouchDown=0;
	private Direction mDir, mNextDir;
	private int mCurrentTileX, mCurrentTileY;
	private float mWidth, mHeight;
	
	

	// ===========================================================
	// Constructors
	// ===========================================================
	
	private Grid grid;
	public Player(final float pX, final float pY, final TiledTextureRegion pTiledTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
		grid = Grid.getInstance();
		 mDir=Direction.left;
		 mNextDir=Direction.left;
		 //move();		
	}

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	
	// ===========================================================
	//Setters & Getters
	public void setNextDirection(Direction dir){
		mNextDir=dir;//mNextDir = dir;                         <---------------------------------------------------------------
	}
	
	public Direction getDirection(){
		return mDir;		
	}
	
	public void setPlayerDimension(int width, int height)
	{
		mWidth = width/30;
		mHeight = height/37;
	}
		
	//obtener centro del player
	public int getCenterX(){
		return (int)(this.getX()+mWidth/2);
	}
	public int getCenterY(){
		return (int) (this.getY() + mHeight/2);
	}
	
	public void setPositionInTile(int x, int y)
	{
		this.setPosition(grid.getTileFromGrid(x, y).getTileCenterX()-mWidth/2, grid.getTileFromGrid(x, y).getTileCenterY()-mHeight/2);
		Log.i("Colocado Tile:", "["+grid.getTileFromGrid(x, y).getTileCenterX()+","+grid.getTileFromGrid(x, y).getTileCenterY()+"]");
	}
	
	
	public void getCurrentTile()
	{
		mCurrentTileX=(int)(getCenterX()/Tile.getWidth())+1;
		mCurrentTileY=(int)(getCenterY()/Tile.getHeight())+1;		
	}
	
	
	// ===========================================================
	// Methods
	// ===========================================================

	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		 getCurrentTile();
		 //Log.i("Casilla actual","["+mCurrentTileX+","+mCurrentTileY+"]:["+getX()+","+getY()+"]");
		 //Log.i("Coordenadas: ","["+getX()+","+getY()+"]");
		 if(!continueMoving())
		 {
			 mPhysicsHandler.setVelocity(0);
		 }												
			
		super.onManagedUpdate(pSecondsElapsed);		
	}
	
	public boolean continueMoving()
	{
		getCurrentTile();
		if (grid.isBlocked(mCurrentTileX,mCurrentTileY,mNextDir))
		{	
			Log.i("Casilla:", "["+mCurrentTileX +","+ mCurrentTileY +"]");
			if(grid.isBlocked(mCurrentTileX,mCurrentTileY,mDir))
			{
				return false;
			}
			else
				return true;			
		}
		else
		{
			mDir=mNextDir;
			//move();
			return true;
		}				
	}
	
	@Override
	public void move() {
		
		
		switch(mDir){
			case right:				
				this.mPhysicsHandler.setVelocityX(mVel);//xVel=100;
				this.mPhysicsHandler.setVelocityY(0);//yVel=0;
			break;
			
			case left:
				this.mPhysicsHandler.setVelocityX(-mVel);//xVel=100;
				this.mPhysicsHandler.setVelocityY(0);//yVel=0;
			break;
			
			case down:
				this.mPhysicsHandler.setVelocityX(0);//xVel=100;
				this.mPhysicsHandler.setVelocityY(mVel);//yVel=0;
			break;
			
			case up:
				this.mPhysicsHandler.setVelocityX(0);//xVel=100;
				this.mPhysicsHandler.setVelocityY(-mVel);//yVel=0;
			break;
		}
		
		
	}
	
	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		
		switch(pSceneTouchEvent.getAction()){
			case (TouchEvent.ACTION_DOWN):
				xTouchDown = pSceneTouchEvent.getX();
				yTouchDown = pSceneTouchEvent.getY();
			break;
			
			case(TouchEvent.ACTION_UP):
				float xTouchUp = pSceneTouchEvent.getX();
				float yTouchUp = pSceneTouchEvent.getY();
				
				float xTouchDistance = xTouchUp - xTouchDown;
				float yTouchDistance = yTouchUp - yTouchDown; 
				
				if(Math.abs(xTouchDistance) > Math.abs(yTouchDistance)){
					if(xTouchDistance > 0)
						setNextDirection(Direction.right);
					else
						setNextDirection(Direction.left);
				}else{
					if(yTouchDistance > 0)
						setNextDirection(Direction.down);
					else
						setNextDirection(Direction.up);
				}
				//this.move();
					
			break;
		}
		return false;
	}

}
